#include <stdio.h>
#include "string.h"
#include "string.h"
#include "driver/gpio.h"
#include "Uart_protocol.h"
#include "esp_log.h"
#include "esp_mac.h"
#include "My_Flash.h"
#include "RTC_SD2506P.h"


#define DEV_TYPE "C_BOARD"
#define MAX_ALLOW_DEV_NUM 100







void _001_Handle_SetDataTime (uart_port_t uart_num , const char *s_datatime)
{
    uint8_t  write_buf[7];
    int i;

    char Msg[200];

    for( i  = 0 ; i < 6 ; i++ ) {
        if (i == 0) {
            write_buf[i] = strtod(s_datatime , NULL);
        } else {
            write_buf[i] = strtod(s_datatime + i*3+2 , NULL);
        }
    }
   
    // esp_err_t err = SD2506AP_SetDataTime(write_buf[0] , write_buf[1] , write_buf[2] ,1 , write_buf[3] ,write_buf[4] ,write_buf[5]) ;
    
    // if (err == ESP_OK) {
    //     sprintf(Msg , "+++ACK%s$" , "Set DataTime OK!");
    // }
    // else {
    //     sprintf(Msg , "+++ACK%s$" , "Set DataTime Error!");
    // }

    // uart_write_bytes( uart_num , Msg , strlen(Msg));
    return;
}

/************************************************************************/
/*    Require the DevID (002), Respond (D03) to send the device id      */
/************************************************************************/
void _002_Handle_DevId( uart_port_t uart_num)
{
    char sMacString[20] = {0};
    uint8_t mac[6];

    if( ESP_OK == esp_efuse_mac_get_default(mac)) {
        sprintf(sMacString ,"+++003%02X%02X%02X%02X%02X%02X$" , mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
        uart_write_bytes( uart_num , (const char*) sMacString , strlen(sMacString));
    }
    return;
}

/************************************************************************/
/*    Require the DevType (004), Respond (005) to send the device type  */
/************************************************************************/
void _004_Handle_SendDevType(uart_port_t uart_num )
{
    char s_Str[100];
    sprintf(s_Str , "+++005%s$" , DEV_TYPE);
    uart_write_bytes( uart_num , s_Str , strlen(s_Str) );
    return;
}

/************************************************************************/
/*    Set device-key (006) ,  Respond to ACK or NCK                     */
/************************************************************************/
void _006_Handle_SetKey(uart_port_t uart_num , const char *Key_value)
{
    char Key_value1[100] = {0};
    char s_ack[220] = {0};
    int i;

    for ( i = 0; i < 100; i++) {
        if (Key_value[i] == '&') break;
        Key_value1[i] = Key_value[i];
    }

    if (i >= 64) return;

    esp_err_t err = nvs_write("Key:" , Key_value1);

    if (err == ESP_OK) {
        sprintf(s_ack , "%s" , "+++ACK-Set Key&");
    } else {
        sprintf(s_ack , "%s" , "+++NCK-Set Key&");
    }
        
    uart_write_bytes( uart_num , s_ack , strlen( s_ack ) );
    return;
}

/*******************************************************************************/
/*    Require the allow DevID-list (007), Respond (008) to send the DevID-list */
/*******************************************************************************/
void _007_Handle_SendDevType(uart_port_t uart_num)
{
    
    char s_Str[100];
    sprintf(s_Str , "+++005%s$" , DEV_TYPE);
    uart_write_bytes( uart_num , s_Str , strlen(s_Str) );
    return;
}

/*******************************************************************************/
/*    Require the allow DevID-list (007), Respond (008) to send the DevID-list */
/*******************************************************************************/

/*******************************************/
void Handle_Protocol( const char *Cmd , const char *dtmp , uart_port_t uart_num , char *c_Cmd)  {

    if ( strncmp(Cmd , "001" , 3) == 0) {                 
       // Handle_DevId_001(uart_num) ;
    } else if ( strncmp(Cmd , "002" , 3) == 0 ) {
        uart_write_bytes( uart_num , "abcd\n" , 5 );
        _002_Handle_DevId(uart_num) ;                     /* Get & send the device id */
    } else if ( strncmp(Cmd , "004" , 3) == 0 ) {
        _004_Handle_SendDevType(uart_num);                /* Get & Send the device type */
    } else if ( strncmp(Cmd , "006" , 3) == 0 ) {
        _006_Handle_SetKey(uart_num , dtmp);              /* Set the device Key */
    } else if ( strncmp(Cmd , "011" , 3) == 0 ) {      /* Get the log*/
      printf("\nzzzzzzzzzzzzzzzzzzz\n");
      *c_Cmd = '1';
    }

    return;
}


 